﻿using System;
using System.Linq;
using System.Web.Mvc;
using MvcContrib.Pagination;
using MvcContrib.Sorting;
using Shp.Web.Models.Contracts;
using Shp.Web.Models.Domain;
using MvcContrib.UI.Grid;

namespace Shp.Web.Controllers
{
    public class GridController : Controller
    {
        #region Fields

        private readonly ICustomerListViewModelBuilder _customerListViewModelBuilder;

        #endregion Fields

        #region Ctors

        public GridController(ICustomerListViewModelBuilder customerListViewModelBuilder)
        {
            _customerListViewModelBuilder = customerListViewModelBuilder;
        }

        #endregion Ctors

        #region Controller Actions Methods

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult ContribGrid(int? page, GridSortOptions sort)
        {
            return View(GetCustomerGrid(page, null, sort, null));
        }

        public ActionResult ThemedGrid(int? page, int? size, GridSortOptions sort)
        {
            var items = GetCustomerGrid(page, size, sort, null);

            if (Request.IsAjaxRequest())
                return PartialView("_ThemedGrid", items);
            return View(items);
        }

        public ActionResult LocalThemedGrid(int? page, int? size, GridSortOptions sort)
        {
            var items = GetCustomerGrid(page, size, sort, null);

            if (Request.IsAjaxRequest())
                return PartialView("_ThemedGrid", items);
            return View(items);
        }

        public ActionResult EmptyThemedGrid(int? page, int? size, GridSortOptions sort)
        {
            var items = GetCustomerGrid(page, size, sort, (c => c.CustomerId > 1000000));

            if (Request.IsAjaxRequest())
                return PartialView("_EmptyThemedGrid", items);
            return View(items);
        }

        public ActionResult SmallThemedGrid(int? page, int? size, GridSortOptions sort)
        {
            var items = GetCustomerGrid(page, size, sort, (c => c.CustomerId < 10));

            if (Request.IsAjaxRequest())
                return PartialView("_SmallThemedGrid", items);
            return View(items);
        }

        #endregion Controller Actions Methods

        #region Controller Non-Action Methods

        [NonAction]
        private IPagination<Customer> GetCustomerGrid(int? page, int? size, GridSortOptions sort,
                                                      Func<Customer, bool> predicate)
        {
            sort.Column = (string.IsNullOrWhiteSpace(sort.Column)) ? "LastName" : sort.Column;
            ViewData["sort"] = sort;

            if (predicate == null)
                return _customerListViewModelBuilder.Build().Customers.OrderBy(sort.Column, sort.Direction).AsPagination
                    (
                        page ?? 1, size ?? 10);
            return _customerListViewModelBuilder.Build().Customers.Where(predicate).OrderBy(sort.Column, sort.Direction)
                .AsPagination(
                    page ?? 1, size ?? 10);
        }

        #endregion Controller Non-Action Methods
    }
}